package com.yuhanginfo.modules.web.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.WebAddress;
import com.yuhanginfo.modules.web.entity.WebArea;
import com.yuhanginfo.modules.web.service.AddressService;
import com.yuhanginfo.modules.web.service.WebAreaService;
import com.yuhanginfo.modules.web.service.WebClassService;

/**
 * 我的地址
 */
@Controller
@RequestMapping(value = "${frontPath}/myAddress")
public class WebMyAddress  {
	   @Autowired
	   private AddressService addressService;
	   @Autowired
	   private WebAreaService webAreaService;
	   @Autowired
	   private WebClassService webClassService;
	   
	   @RequestMapping(value = "/myAddressList")
       public String myAddressList(Model model){
		   if(WebUserUtils.getYongUser().getId()!=null&&WebUserUtils.getYongUser().getId()!=""){
    	   List<WebAddress> list=addressService.selectUserId(WebUserUtils.getYongUser().getId());
    	   List<WebArea> provinceList=webAreaService.selectProvinceList();
  		   model.addAttribute("provinceList", provinceList);
    	   model.addAttribute("list", list);
    	   model.addAttribute("listSize", list.size());
    	   model.addAttribute("allClassesList",webClassService.getAllClasses());
    	   model.addAttribute("actionCenter", "addr");
    	   return "modules/web/Order/myAddress";
		   }else{
			   return "modules/web/webLogin";  
		   }
       }
	   
	 
	  @RequestMapping(value = "/sheAddress")
	  public String sheAddress(String id){
		  List<WebAddress> list=addressService.selectUserId(WebUserUtils.getYongUser().getId());
		  for (WebAddress addr : list) {
			   if(addr.getId().equals(id)){
				  WebAddress address=new WebAddress();
				  address.setId(id);
				  address.setIsDef("1");
				  address.setUpdateDate(new Date());
				  addressService.updateDeauft(address);
			   }else{
				  if("1".equals(addr.getIsDef())){
				  WebAddress address=new WebAddress();
				  address.setId(addr.getId());
				  address.setIsDef("0");
				  address.setUpdateDate(new Date());
				  addressService.updateDeauft(address);
				  }
			   }
		  }
		  
		  return "redirect:"+Global.getFrontPath()+"/myAddress/myAddressList";
	  } 
	   
	 //删除地址
	 @RequestMapping(value = "/deleteAddress")
	 public String  deleteAddress(String id){
		 addressService.deleteAddress(id);
		 return "redirect:"+Global.getFrontPath()+"/myAddress/myAddressList";
	 }
	 
	@ResponseBody
	@RequestMapping(value = "/cityList") 
	public  List<WebArea> cityList(String id){
		return webAreaService.cityList(id);
	}
	
	@ResponseBody
	@RequestMapping(value = "/areaList") 
	public  List<WebArea> areaList(String id){
		return webAreaService.areaList(id);
	}
	
	
	//新增与编辑收货地址
	@RequestMapping(value = "/addSave") 
	public String addSave(WebAddress address){
		List<WebAddress> list=addressService.selectUserId(WebUserUtils.getYongUser().getId());
		if(address.getId()==null||address.getId()==""){
		if("0".equals(address.getIsDef())){
		address.setUserId(Integer.parseInt(WebUserUtils.getYongUser().getId()));
		address.setUpdateDate(new Date());
		address.setCreateDate(new Date());
		addressService.saveMyAddress(address);
		}else{
			for (WebAddress address2 : list) {
				if("1".equals(address2.getIsDef())){
					WebAddress addr=new WebAddress();
					addr.setIsDef("0");
					addr.setUpdateDate(new Date());
					addr.setId(address2.getId());
					addressService.updateDeauft(addr);
				}
			}
			address.setUserId(1);
			address.setUpdateDate(new Date());
			address.setCreateDate(new Date());
			addressService.saveMyAddress(address);
		}
		}else{  
			   //当参数为默认值时
				if("1".equals(address.getIsDef())){
					for (WebAddress address2 : list) {
						//判断列表里面的值是当前修改的值时
						if(address2.getId().equals(address.getId())){
							WebAddress addr=new WebAddress();
							addr.setIsDef("1");
							addr.setUpdateDate(new Date());
							addr.setId(address2.getId());
							addressService.updateDeauft(addr);
						}else{
							if("1".equals(address2.getIsDef())){
								WebAddress addr=new WebAddress();
								addr.setIsDef("0");
								addr.setUpdateDate(new Date());
								addr.setId(address2.getId());
								addressService.updateDeauft(addr);
							}
						}
					}
					
				}else{
					WebAddress addr=new WebAddress();
					addr.setIsDef("0");
					addr.setUpdateDate(new Date());
					addr.setId(address.getId());
					addressService.updateDeauft(addr);
					
				}
		}
		return "redirect:"+Global.getFrontPath()+"/myAddress/myAddressList";
	}
	
	//弹框页面显示
	@ResponseBody
	@RequestMapping(value = "/showAddressDetail") 
	public  WebAddress showAddressDetail(String id){
		return addressService.get(id);
	}
	
	 //手机地址列表
	 @RequestMapping(value = "/myPhoneAddressList")
     public String myPhoneAddressList(Model model){
	  if(WebUserUtils.getYongUser().getId()!=null&&WebUserUtils.getYongUser().getId()!=""){
  	   List<WebAddress> list=addressService.selectPhoneUserId(WebUserUtils.getYongUser().getId());
  	   List<WebArea> provinceList=webAreaService.selectProvinceList();
	   model.addAttribute("provinceList", provinceList);
  	   model.addAttribute("list", list);
  	   model.addAttribute("listSize", list.size());
  	   model.addAttribute("allClassesList",webClassService.getAllClasses());
  	   return "modules/web/Order/myPhoneAddress";
	  }else{
		   return "modules/web/webLogin"; 
		  
		 }
     }
	 
	//手机设置默认地址
	 @RequestMapping(value = "/shePhoneAddress")
	  public String shePhoneAddress(String id){
		  List<WebAddress> list=addressService.selectUserId(WebUserUtils.getYongUser().getId());
		  for (WebAddress addr : list) {
			   if(addr.getId().equals(id)){
				  WebAddress address=new WebAddress();
				  address.setId(id);
				  address.setIsDef("1");
				  address.setUpdateDate(new Date());
				  addressService.updateDeauft(address);
			   }else{
				  if("1".equals(addr.getIsDef())){
				  WebAddress address=new WebAddress();
				  address.setId(addr.getId());
				  address.setIsDef("0");
				  address.setUpdateDate(new Date());
				  addressService.updateDeauft(address);
				  }
			   }
		  }
		  
		  return "redirect:"+Global.getFrontPath()+"/myAddress/myPhoneAddressList";
	   } 
	 
	    //手机删除地址
		 @RequestMapping(value = "/deletePhoneAddress")
		 public String  deletePhoneAddress(String id){
			 addressService.deleteAddress(id);
			 return "redirect:"+Global.getFrontPath()+"/myAddress/myPhoneAddressList";
		 }
		 
	  //新增与编辑收货地址
	  @RequestMapping(value = "/addPhoneSave") 
		public String addPhoneSave(WebAddress address){
			List<WebAddress> list=addressService.selectUserId(WebUserUtils.getYongUser().getId());
			if(address.getId()==null||address.getId()==""){
			if("0".equals(address.getIsDef())){
			address.setUserId(Integer.parseInt(WebUserUtils.getYongUser().getId()));
			address.setUpdateDate(new Date());
			address.setCreateDate(new Date());
			addressService.saveMyAddress(address);
			}else{
				for (WebAddress address2 : list) {
					if("1".equals(address2.getIsDef())){
						WebAddress addr=new WebAddress();
						addr.setIsDef("0");
						addr.setUpdateDate(new Date());
						addr.setId(address2.getId());
						addressService.updateDeauft(addr);
					}
				}
				address.setUserId(1);
				address.setUpdateDate(new Date());
				address.setCreateDate(new Date());
				addressService.saveMyAddress(address);
			}
			}else{  
				   //当参数为默认值时
					if("1".equals(address.getIsDef())){
						for (WebAddress address2 : list) {
							//判断列表里面的值是当前修改的值时
							if(address2.getId().equals(address.getId())){
								WebAddress addr=new WebAddress();
								addr.setIsDef("1");
								addr.setUpdateDate(new Date());
								addr.setId(address2.getId());
								addressService.updateDeauft(addr);
							}else{
								if("1".equals(address2.getIsDef())){
									WebAddress addr=new WebAddress();
									addr.setIsDef("0");
									addr.setUpdateDate(new Date());
									addr.setId(address2.getId());
									addressService.updateDeauft(addr);
								}
							}
						}
						
					}else{
						WebAddress addr=new WebAddress();
						addr.setIsDef("0");
						addr.setUpdateDate(new Date());
						addr.setId(address.getId());
						addressService.updateDeauft(addr);
						
					}
			}
			return "redirect:"+Global.getFrontPath()+"/myAddress/myPhoneAddressList";
		}
	 
	 //手机分页下拉
	  @RequestMapping(value="/addrPhoneList")
		public String addrPhoneList(Integer p,Model model) {
			Map<String, Object> map = new HashMap<String, Object>();
			int a=4*(p-1);
			map.put("a",a);
			map.put("userId", WebUserUtils.getYongUser().getId());
			
			List<WebAddress> list=addressService.selectPhoneList(map);
			model.addAttribute("list", list);
			return "modules/web/Order/myPhoneAddress2";
	  }	
	  
}
